function RolePanel() {
};
// 静态
RolePanel.instance = RolePanel;

RolePanel.faceUrl = "roleController";

/**
 * 分页条
 */
RolePanel.rolePager = null;

/**
 * 角色表格
 */
RolePanel.roleDatagrid = null;

/**
 * 管理角色窗口
 */
RolePanel.roleEditWindow = null;

/**
 * 角色授权窗口
 */
RolePanel.authorizedWin = null;

/**
 * 角色功能树
 */
RolePanel.funTreegrid = null;

/**
 * 角色下用户表
 */
RolePanel.roleUserDatagrid = null;

/**
 * 设置角色下用户的用户表窗口
 */
RolePanel.roleRoleUserEditWindow = null;

/**
 * 设置角色下用户的用户表
 */
RolePanel.roleUserWinDatagrid = null;

/**
 * 设置角色下用户的用户表分页条
 */
RolePanel.roleUserPager = null;

/**
 * 初始
 */
RolePanel.init = function(){
	
	RolePanel.loadToolbar();
	
	RolePanel.rolePager = mini.get("rolePager");
	RolePanel.roleDatagrid = mini.get("roleDatagrid");
	RolePanel.roleEditWindow = mini.get("roleEditWindow");
	RolePanel.authorizedWin = mini.get("roleRoleEditWindow");
	RolePanel.funTreegrid = mini.get("roleTreegrid");
	RolePanel.roleUserDatagrid = mini.get("roleUserDatagrid");
	RolePanel.roleRoleUserEditWindow = mini.get("roleRoleUserEditWindow");
	RolePanel.roleUserWinDatagrid = mini.get("roleUserWinDatagrid");
	RolePanel.roleUserPager = mini.get("roleUserPager");
	
	RolePanel.renderer();
	RolePanel.reload();
};

/**
 * 加载数据
 */
RolePanel.reload = function() {
	RolePanel.loadData(RolePanel.rolePager.pageIndex, RolePanel.rolePager.pageSize);
	RolePanel.roleUserDatagrid.loadData([]);
};

/**
 * 加载按钮
 */
RolePanel.loadToolbar = function() {
	var array = new Array("system_role_add", "system_role_edit", "system_role_del",
			"system_role_author", "system_role_reload", "system_role_bund");
	var array2 = MiniuiTools.isHasPurview(array);
	for ( var i = 0; i < array2.length; i++) {
		mini.get(array[i]).setVisible(array2[i]);
	}
};

/**
 * 加载角色下用户表
 * @return
 */
RolePanel.loadRoleUser = function(obj) {
	
	var role = obj.selected ? obj.selected : obj;
	if (role) {
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			cache: false,
			url: __root+"/"+RolePanel.faceUrl,
			data: {
				method: "getUserListByRoleId",
				roleId: role.roleId
			},
			success: function(json) {
				if (json && json.length > 0) {
					RolePanel.roleUserDatagrid.loadData(json);
				} else {
					RolePanel.roleUserDatagrid.loadData([]);
				}
				return;
			},
			error: function(obj, ertxt) {
				mini.alert("系统错误：" + ertxt);
			}
		});
	}
};

/**
 * 加载全部用户
 * @param pageIndex  页码
 * @param pageSize  每页多少条
 */
RolePanel.loadUser = function(pageIndex, pageSize){
	
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+'userController',
		cache: false,
		data: {
			method: "find",
			size: pageSize,
			pageNo: pageIndex
		},
		success: function(json) {
			if (json) {
				RolePanel.roleUserPager.setTotalCount(json.rowCount);
				RolePanel.roleUserWinDatagrid.loadData(json.resultList);
				return;
			}
			return;
		},
		error: function(obj, ertxt) { /* 失败回调函数.这里是网络失败, 比如500错误等 */
			//mini.hideMessageBox(messageId);
			mini.alert("初始化数据错误：" + ertxt);
		}
	});
};

/**
 * 对表格数据渲染
 */
RolePanel.renderer = function(){
	RolePanel.roleDatagrid.on("drawcell", function (e){
		var  field = e.field, value = e.value;
		if(field == "name"){e.cellHtml = "<a href='javascript:RolePanel.edit();'>[" + value + "]</a>";}
		if(field == "glText"){e.cellHtml = "<a href='javascript:RolePanel.authorized();'>【授权】</a> <a href=\"javascript:RolePanel.del();\">【删除】</a>";}
	});
};

/**
 * 新增角色按钮事件
 */
RolePanel.add = function(){
	var form = new mini.Form("#roleEditform");
	form.clear();
	RolePanel.roleEditWindow.show();
};

/**
 * 编辑角色按钮事件
 */
RolePanel.edit = function(){
	
	if (!MiniuiTools.hasPurview("system_role_edit")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = RolePanel.roleDatagrid.getSelected();
	
	var form = new mini.Form("#roleEditform");
	form.clear();
	if(rowData){
		form.setData(rowData);
		RolePanel.roleEditWindow.show();
	}else{
		mini.alert("请选中要修改的角色");
	}
};

/**
 * 删除角色
 */
RolePanel.del = function() {
	
	if (!MiniuiTools.hasPurview("system_role_del")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = RolePanel.roleDatagrid.getSelected();
	
	if(!rowData){
		mini.alert("请选中要删除的记录！");
	}else{
		mini.confirm("确定要删除角色名为  " + "<span style='color: red;'> " +rowData.name + "</span>" + "  的用户吗？", "删除用户",
	            function (action) {      
	                if (action == "ok") {
	                	var ids = new Array();
	            		ids.push(rowData.roleId);
	            		
	            		$.ajax( {
	            			type: 'POST',
	            			dataType: 'json',
	            			url: __root+"/"+RolePanel.faceUrl,
	            			data: {
	            				method: 'delete',
	            				roleId: ids,
	            				lslUser: gbOperateUser,
				    			lslAddress: gbOperateIp
	            			},
	            			success: function(text) {
	            				RolePanel.reload();
	            			},
	            			error: function(obj, error) {
	            				RolePanel.reload();
	            			}
	            		});
	                }
		});
	}
};

/**
 * 角色授权按钮
 */
RolePanel.authorized = function(){
	
	if (!MiniuiTools.hasPurview("system_role_author")) {
		mini.showMessageBox({title:"提示",message:"糟糕，您没有权限访问！",iconCls:"mini-messagebox-info",buttons: ["ok"]});
		return;
	}
	
	var rowData = RolePanel.roleDatagrid.getSelected();
	
	if(rowData){
		
		//加载树的数据
		RolePanel.loadFunTree(rowData.roleId);
		RolePanel.authorizedWin.show();
		
	}else{
		mini.alert("请选中要授权的角色");
	}
};

/**
 * 加载用户数据
 */
RolePanel.reloadUser = function(){
	RolePanel.loadUser(RolePanel.roleUserPager.pageIndex, RolePanel.roleUserPager.pageSize);
};

/**
 * 设置角色用户
 */
RolePanel.showUserSet = function(){
	
	var rowData = RolePanel.roleDatagrid.getSelected();
	if(!rowData){
		mini.alert("请选中要设置用户的角色！");
		return;
	}else{
		RolePanel.reloadUser();
		RolePanel.roleRoleUserEditWindow.show();
	}
};

/**
 * 关闭编辑窗口按钮
 */
RolePanel.closeButton = function(){
	RolePanel.roleEditWindow.hide();
};

/**
 * 关闭授权窗口按钮
 */
RolePanel.closeRoButton = function(){
	RolePanel.authorizedWin.hide();
};

/**
 * 关闭设置角色用户窗口按钮
 */
RolePanel.closeRoleUserButton = function(){
	RolePanel.roleRoleUserEditWindow.hide();
};

/**
 * 页码改变事件
 */
RolePanel.pagechanged = function(e){
	RolePanel.loadData(e.pageIndex, e.pageSize);
};

/**
 * 页码改变事件2
 */
RolePanel.userPagechanged = function(e){
	RolePanel.loadUser(e.pageIndex, e.pageSize);
};

/**
 * 传入页数和每页显示树查找数据
 */
RolePanel.loadData = function(index, size){
	   RolePanel.query( {
	  		pageNo: index,
	  		size: size
	  	}, function(data) {
	  		RolePanel.rolePager.setTotalCount(data.rowCount);
	  		RolePanel.roleDatagrid.loadData(data.resultList);
	  	});
};

/**
 * 首先加载所有功能列表，然后选中select角色具有的功能列表
 * @return
 */
RolePanel.loadFunTree = function(roleId) {
	
	var fns = [];
	for ( var i = 0; i < parent.ClientData.allFunNodeData.length; i++) {
		fns.push(parent.ClientData.allFunNodeData[i]);
	}
	RolePanel.funTreegrid.loadData(fns);
	
	var param = {
			method: 'getRoleFunNodeRelation',
			roleId: roleId
		};

		$.ajax( {
			type: 'POST',
			dataType: 'json',
			url: __root+"/"+RolePanel.faceUrl,
			data: param,
			success: function(json) {
				var nodes = [];
				for ( var i = 0; i < json.length; i++) {
					var funNode = MiniuiTools.findTreeNode(json[i].funId, fns, null, "funId",99);
					nodes.push(funNode);
				}
				RolePanel.funTreegrid.uncheckAllNodes();
				RolePanel.funTreegrid.collapseAll(); 
				RolePanel.funTreegrid.checkNodes(nodes);
				return;
			},
			error: function(e, errtxt) {
				mini.alert("系统错误：" + errtxt);
				return;
			}
		});
	};

/**
 * 查询角色信息
 */
RolePanel.query = function(params, success){
	params.method = 'find';
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+RolePanel.faceUrl,
		cache: false,
		data: params,
		success: function(json) {
			if (json) {
				if (json) {
					success(json);
				}
				return;
			}
			return;
		},
		error: function(obj, ertxt) { /* 失败回调函数.这里是网络失败, 比如500错误等 */
			mini.alert("初始化数据错误：" + ertxt);
		}
	});
};

/**
 * 保存
 */
RolePanel.save = function() {
	
	var form = new mini.Form("#roleEditform");
	
	form.validate();
    if (form.isValid() == false) return;
	
	var data = form.getData();
	data.method = "save";
	data.lslUser = gbOperateUser;
	data.lslAddress = gbOperateIp;

	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+RolePanel.faceUrl,
		data: data,
		success: function(text) {
			RolePanel.reload();
			RolePanel.closeButton();
			return;
		},
		error:function(){
			mini.alert("保存出错！");
			RolePanel.closeButton();
		}
	});
};

/**
 * 保存角色授权信息
 */
RolePanel.savePurview = function() {
	var sels = RolePanel.funTreegrid.getCheckedNodes();
	var param = {
		method: 'setPurview',
		roleId: '',
		roleName: '',
		funId: [],
		funName: []
	};
	var data = RolePanel.roleDatagrid.getSelected();
	param.roleId = data.roleId;
	param.roleName = data.name;
	for ( var i = 0, l = sels.length; i < l; i++) {
		param.funId.add(sels[i].funId);
		param.funName.add(sels[i].name);
	}

	param.lslUser = gbOperateUser;
	param.lslAddress = gbOperateIp;
	
	$.ajax( {
		type: 'POST',
		dataType: 'json',
		url: __root+"/"+RolePanel.faceUrl,
		data: param,
		success: function(text) {
			RolePanel.closeRoButton();
			RolePanel.reload();
			return;
		},
		error: function(e, errtxt) {
			mini.alert("系统错误：" + errtxt);
			RolePanel.closeRoButton();
			RolePanel.reload();
			return;
		}
	});
};

/**
 * 设置用户到角色
 */
RolePanel.saveRoleUser = function() {
	
	var rowData = RolePanel.roleUserWinDatagrid.getSelecteds();
	var role = RolePanel.roleDatagrid.getSelected();
	
	if(!rowData){
		mini.alert("请选中要设置的用户！");
		return;
	}
	
	var userId = [];
	
		for ( var i = 0; i < rowData.length; i++) {
			userId.push(rowData[i].userId);
		}
		var data = {
			method: 'saveUserRole',
			roleId: role.roleId,
			userId: userId,
			lslUser: gbOperateUser,
			lslAddress: gbOperateIp
		};
		
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			url: __root+"/"+RolePanel.faceUrl,
			data: data,
			success: function(json) {
				RolePanel.loadRoleUser(role);
				RolePanel.closeRoleUserButton();
				return;
			},
			error: function(e, etxt) {
				mini.alert("系统错误：" + errtxt);
				RolePanel.closeRoleUserButton();
				return;
			}
		});
};


/**
 * 删除角色用户
 * @return
 */
RolePanel.delRoleUser = function() {
	var v = RolePanel.roleUserDatagrid.getSelecteds();
	var role = RolePanel.roleDatagrid.getSelected();
	
	if (v && v.length > 0 && role) {
		var userId = [];
		for ( var i = 0; i < v.length; i++) {
			userId.push(v[i].userId);
		}
		var data = {
			method: 'delRoleUser',
			roleId: role.roleId,
			userId: userId,
			lslUser: gbOperateUser,
			lslAddress: gbOperateIp
		};
		
		$.ajax( {
			type: 'POST',
			dataType: 'json',
			url:  __root+"/"+RolePanel.faceUrl,
			data: data,
			success: function(json) {
				RolePanel.loadRoleUser(role);
				return;
			},
			error: function(e, etxt) {
				mini.alert("系统错误：" + etxt);
				RolePanel.loadRoleUser(role);
				return;
			}
		});
	} else {
		mini.alert("请选择要取消的角色用户！");
	}
};